<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/7
 * Time: 下午4:05
 */

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\Room;
use App\Http\Model\RoomMelee;


class RoomMeleeServices extends CommonServices
{
    private $modelRoomMelee;
    private $modelRoom;
    private $melee_status = [0,1];

    public function __construct()
    {
        $this->modelRoom = new Room();
        $this->modelRoomMelee = new RoomMelee();
        parent::__construct();
    }




    /**
     * 开启团战
     * @return array
     */
    public function open()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('room_id', '房间ID', 'int', true, '房间ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }

            $roomId = $this->requestData['room_id'];
            $curUser = $this->user->get_current_user();
            if ($this->modelRoom->get_role($roomId, $curUser['user_id']) == 0) {
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            $this->modelRoomMelee->setState($roomId, 1);

        } while (false);

        return compact('code', 'msg', 'data');

    }

    /**
     * 关闭团战
     * @return array
     */
    public function close()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('room_id', '房间ID', 'int', true, '房间ID不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }

            $roomId = $this->requestData['room_id'];
            $curUser = $this->user->get_current_user();
            if ($this->modelRoom->get_role($roomId, $curUser['user_id']) == 0) {
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            $this->modelRoomMelee->setState($roomId, 0);

        } while (false);

        return compact('code', 'msg', 'data');
    }


    public function setMelee($input)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $room_id = $input['room_id'];
            $melee_status = $input['melee'] ?? 0;
            if(!in_array($melee_status,$this->melee_status)){
                $melee_status = 0;
            }
            $res = $this->modelRoom->setMelee($room_id,$melee_status);
            if(!$res){
                $code = Hint::ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
        } while (false);
        return compact('code', 'msg', 'data');
    }

    public function setTally($input)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $room_id = $input['room_id'];
            $tally_status = $input['tally'] ?? 0;
            if(!in_array($tally_status,$this->melee_status)){
                $tally_status = 0;
            }
            $res = $this->modelRoom->setTally($room_id,$tally_status);
            if(!$res){
                $code = Hint::ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
        } while (false);
        return compact('code', 'msg', 'data');
    }

}